<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace addons\equip\user\controller;

use addons\equip\model\Equip as EquipM;

/**
 * Description of Equip
 * 装备
 * @author zmh
 */
class Equip extends \web\user\controller\AddonUserBase {


    public function index() {
        return $this->fetch();
    }

    /**
     * 装备列表
     * @return array
     */
    public function loadList() {
        $keyword = $this->_get('keyword');
        $filter = ' 1=1';
        if ($keyword != null) {
            $filter .= ' and equip_name like \'%' . $keyword . '%\'';
        }
        $m = new EquipM;
        $total = $m->getTotal($filter);
        $rows = $m->getList($this->getPageIndex(), $this->getPageSize(), $filter, 'id asc');
        return $this->toDataGrid($total, $rows);
    }

    /**
     * 装备添加和修改
     * @return array|mixed|string
     */
    public function edit() {
        if (IS_POST) {
            $data = $_POST;
            $id = $data['id'];
            $data['update_time'] = NOW_DATETIME;
            $m = new EquipM;
            try {
                if (empty($id)) {
                    unset($data['id']);
                    $data['create_time'] = NOW_DATETIME;
                    $ret = $m->add($data, false);
                    $this->addLog(self::LOG_ADD,'跑鞋添加',$data, '');
                } else {
                    $where['id'] = $id;
                    $ret = $m->save($data, $where, null, false);
                    $this->addLog(self::LOG_EDIT,'跑鞋修改',$data, '');
                }
                //添加管理日志
                return $this->successData();
            } catch (\Exception $e) {
                return $this->failData($e->getMessage());
            }
        } else {
            $id = $this->_get('id');
            $this->assign('id', $id);
            $this->setLoadDataAction('loadData');
            return $this->fetch();
        }
    }


    public function loadData() {
        $id = $this->_get('id');
        $m = new EquipM;
        $data = $m->getDetail($id);
        return $data;
    }

    /**
     * 删除
     */
    public function del() {
        $id = intval($this->_get('id'));
        if (!empty($id)) {
            $m = new EquipM;
            try {
                $res = $m->deleteData($id);
                if ($res > 0) {
                    //添加管理日志
                    $this->addLog(self::LOG_EDIT, 'Equip', [], 0, '装备管理删除');
                    return $this->successData();
                } else {
                    return $this->failData('删除失败');
                }
            } catch (\Exception $e) {
                return $this->failData($e->getMessage());
            }
        } else {
            return $this->failData('删除失败，参数有误');
        }
    }

}
